package com.ldu.service.impl;

import javax.annotation.Resource;

import com.ldu.dao.dataobject.AdminDO;
import com.ldu.dao.intf.AdminManager;
import com.ldu.dao.query.AdminQuery;
import com.ldu.dao.query.support.PageResult;
import com.ldu.enums.CommonStatusEnum;
import com.ldu.util.BeanUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ldu.dao.mapper.AdminMapper;
import com.ldu.pojo.Admin;
import com.ldu.service.AdminService;

@Service(value="adminService")
public class AdminServiceImpl implements AdminService {
	@Resource
	private AdminMapper adminMapper;

	@Resource
	private AdminManager adminManager;

	@Override
	public Admin findAdmin(Long phone, String password) {
		AdminQuery adminQuery = new AdminQuery();
		adminQuery.createCriteria()
				.andPhoneEqualTo(phone)
				.andPasswordEqualTo(password)
				.andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
		List<AdminDO> adminDOS = adminManager.selectByQuery(adminQuery);
		if (CollectionUtils.isEmpty(adminDOS)) {
			return  null;
		}
		return BeanUtil.createCloneBean(adminDOS.get(0), Admin.class);
	}

	@Override
	public Admin findAdminById(Long id) {
		AdminDO adminDO = adminManager.selectByPrimaryKey(id);
		return BeanUtil.createCloneBean(adminDO, Admin.class);
	}

	@Override
	public void updateAdmin(Admin admins) {
		AdminDO adminDO = BeanUtil.createCloneBean(admins, AdminDO.class);
		adminManager.updateByPrimaryKeySelective(adminDO);
	}

	@Override
	public List<Admin> getAdminList() {
		AdminQuery adminQuery = new AdminQuery();
		adminQuery.createCriteria().andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
		List<AdminDO> adminDOS = adminManager.selectByQuery(adminQuery);
		if (CollectionUtils.isEmpty(adminDOS)) {
			return new ArrayList<>();
		}
		return adminDOS.stream().map(adminDO -> {
			Admin admin = BeanUtil.createCloneBean(adminDO, Admin.class);
			admin.setUserRoleName(CommonStatusEnum.getRoleNameByCode(admin.getUserRole()));
			return admin;
		}).collect(Collectors.toList());
	}

	/*@Override
	public void insertSelective(Admin admin) {
		adminMapper.insertSelective(admin);

	}*/

//	@Override
//	public int getAdminNum() {
//		List<Admin> admin= adminMapper.selectAdminList();
//		return admin.size();
//	}


	@Override
	public PageResult<Admin> getPageAdmin(int pageNum, int pageSize) {
		AdminQuery adminQuery = new AdminQuery();
		adminQuery.setPageNo(pageNum);
		adminQuery.setPageSize(pageSize);
		adminQuery.createCriteria().andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
		PageResult<AdminDO> adminDOPageResult = adminManager.selectByQueryWithPage(adminQuery);
		if (CollectionUtils.isEmpty(adminDOPageResult.getResult())) {
			return PageResult.create("无数据");
		}
		List<Admin> admins = adminDOPageResult.getResult().stream().map(adminDO -> {
			Admin admin = BeanUtil.createCloneBean(adminDO, Admin.class);
			admin.setUserRoleName(CommonStatusEnum.getRoleNameByCode(admin.getUserRole()));
			return admin;
		}).collect(Collectors.toList());
		return PageResult.createPageResult(admins, adminDOPageResult.getPageInfo());
	}


	@Override
	public PageResult<Admin> getPageAdminByIdOrUserName(Long id, String username, Integer userRole, int pageNum, int pageSize) {
		AdminQuery adminQuery = new AdminQuery();
		adminQuery.setPageNo(pageNum);
		adminQuery.setPageSize(pageSize);
		AdminQuery.Criteria criteria = adminQuery.createCriteria();
		//判断是否为空 避免查询冲突  避免在查询时其他条件为空时 查询失败
		if (null != id) {
			criteria.andIdEqualTo((long)id);
		}
		if (StringUtils.isNotBlank(username)) {
			criteria.andUsernameLike("%"+username+"%");
		}
		if (null != userRole) {
			criteria.andUserRoleEqualTo(userRole);
		}
		PageResult<AdminDO> adminDOPageResult = adminManager.selectByQueryWithPage(adminQuery);

		List<Admin> admins = adminDOPageResult.getResult().stream().map(adminDO -> BeanUtil.createCloneBean(adminDO, Admin.class)).collect(Collectors.toList());
		return PageResult.createPageResult(admins, adminDOPageResult.getPageInfo());
	}

	@Override
	public Admin getAdminById(int id) {
		AdminDO adminDO = adminManager.selectByPrimaryKey((long) id);
		return BeanUtil.createCloneBean(adminDO, Admin.class);
	}

	@Override
	public int updateByPrimaryKey(Admin admin) {
		AdminDO adminDO = BeanUtil.createCloneBean(admin, AdminDO.class);
		return  adminManager.updateByPrimaryKeySelective(adminDO);
	}

	@Override
	public Boolean deleteAdminById(Long id) {
		AdminDO adminDO = adminManager.selectByPrimaryKey(id);
		if (null == adminDO) {
			return false;
		}
		adminDO.setIsDeleted(CommonStatusEnum.YES.getCode());
		int i = adminManager.updateByPrimaryKeySelective(adminDO);

		return i == 1;
	}


	@Override
	public Admin selectByPrimaryKey(Long id){
		AdminDO adminDO = adminManager.selectByPrimaryKey(id);
		return BeanUtil.createCloneBean(adminDO, Admin.class);
	}

	@Override
	public List<Admin> queryAdmin(Admin admin) {
		AdminQuery query = buildAdminQuery(admin);
		List<AdminDO> adminDOS = adminManager.selectByQuery(query);
		return adminDOS.stream().map(adminDO -> {
			Admin cloneBean = BeanUtil.createCloneBean(adminDO, Admin.class);
			cloneBean.setUserRoleName(CommonStatusEnum.getRoleNameByCode(cloneBean.getUserRole()));
			return cloneBean;
		}).collect(Collectors.toList());
	}

	@Override
	public Boolean add(Admin admin) {
		AdminDO adminDO = BeanUtil.createCloneBean(admin, AdminDO.class);
		adminDO.setIsDeleted(CommonStatusEnum.NO.getCode());
		long l = adminManager.insertSelective(adminDO);
		return l == 1;
	}

	private AdminQuery buildAdminQuery(Admin admin) {
		AdminQuery query = new AdminQuery();
		if (null == admin) {
			return query;
		}

		AdminQuery.Criteria criteria = query.createCriteria().andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
		if (null != admin.getId()) {
			criteria.andIdEqualTo(admin.getId());
		}
		if (StringUtils.isNotBlank(admin.getUsername())) {
			criteria.andUsernameEqualTo(admin.getUsername());
		}
		if (null != admin.getPhone()) {
			criteria.andPhoneEqualTo(admin.getPhone());
		}

		return query;
	}
}
